package com.fc.happyteam.api;

import com.fc.happyteam.common.ResponseJson;
import com.fc.happyteam.entity.RegionEntity;
import com.fc.happyteam.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fc.happyteam.utils.tree.MultipleTreeUtil.buildTreeNode;

@RestController
@RequestMapping("/api/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseJson viewOrderList() {
        ResponseJson responseJson = new ResponseJson();
        List<RegionEntity> regionList = regionService.queryList(new HashMap<>());
        if (regionList != null && regionList.size() > 0) {
            List<Map<String, Object>> dataList = new ArrayList<>(regionList.size());
            for (RegionEntity region : regionList) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", region.getId());
                map.put("label", region.getLabel());
                map.put("name", region.getName());
                map.put("parentId", region.getParentId());

                dataList.add(map);
            }
            responseJson.setObj(buildTreeNode(dataList, true));
        }

        return responseJson;
    }
}
